package com.frxuexi.park2209.mynews

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209.R
import com.frxuexi.park2209.databinding.ActivityMyNewsBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MyNewsActivity : BaseActivity<ActivityMyNewsBinding,MyNewsViewModel>() {
    val mynewsAdapter by lazy { MyMewsAdapter() }
    override fun initData() {
        /*lifecycleScope.launch {
            vm.news.collect{
                if(it.isNotEmpty()){
                    val json = Gson().fromJson(it, Any::class.java)
                    Log.i("===",json.toString())
                }
            }
        }*/
    }

    override fun initView() {
        binding.headerNews.headerTitle.text="文化管理"
        binding.headerNews.headerAdd.visibility=View.VISIBLE
        binding.headerNews.headerAdd.setOnClickListener {
            startActivity(Intent(this@MyNewsActivity,AddNewsActivity::class.java))
        }
        binding.headerNews.ivBack.setOnClickListener {
            finish()
        }

        binding.rvNews.apply {
            adapter=mynewsAdapter
            layoutManager=LinearLayoutManager(this@MyNewsActivity)
        }

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getNews().collect{
                mynewsAdapter.submitData(it)
            }
        }
    }

}